home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / utility1 / gs261src.zip / ZMATRIX.C < prev    next >
C/C++ Source or Header  |  1993-05-13  |  7KB  |  269 lines

  1. /* Copyright (C) 1989, 1991, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* zmatrix.c */
  20. /* Matrix operators for Ghostscript */
  21. #include "ghost.h"
  22. #include "errors.h"
  23. #include "oper.h"
  24. #include "gsmatrix.h"
  25. #include "state.h"
  26. #include "gscoord.h"
  27. #include "store.h"
  28.  
  29. /* Forward references */
  30. private int near common_transform(P3(os_ptr,
  31.   int (*)(P4(gs_state *, floatp, floatp, gs_point *)),
  32.   int (*)(P4(floatp, floatp, const gs_matrix *, gs_point *))));
  33.  
  34. /* Initialize the type and attributes of the identity matrix. */
  35. /* We may have to do this after each save and restore, */
  36. /* in order to get the l_new attribute correct. */
  37. void
  38. init_identity_matrix(void)
  39. {    extern gs_matrix gs_identity_matrix;
  40.     ref *mp = (ref *)&gs_identity_matrix;
  41.     int attrs = alloc_save_new_mask;
  42.     int i;
  43.     for ( i = 0; i < 6; i++, mp++ )
  44.         r_set_type_attrs(mp, t_real, attrs);
  45. }
  46.  
  47. /* <matrix> currentmatrix <matrix> */
  48. int
  49. zcurrentmatrix(register os_ptr op)
  50. {    int code = write_matrix(op);
  51.     if ( code < 0 )
  52.         return code;
  53.     gs_currentmatrix(igs, (gs_matrix *)(op->value.refs));
  54.     return 0;
  55. }
  56.  
  57. /* <matrix> setmatrix - */
  58. int
  59. zsetmatrix(register os_ptr op)
  60. {    gs_matrix mat;
  61.     int code = read_matrix(op, &mat);
  62.     if ( code < 0 )
  63.         return code;
  64.     if ( (code = gs_setmatrix(igs, &mat)) < 0 )
  65.         return code;
  66.     pop(1);
  67.     return 0;
  68. }
  69.  
  70. /* <tx> <ty> translate - */
  71. /* <tx> <ty> <matrix> translate <matrix> */
  72. int
  73. ztranslate(register os_ptr op)
  74. {    int code = write_matrix(op);
  75.     float trans[2];
  76.     if ( code < 0 )            /* no matrix operand */
  77.     {    if ( (code = num_params(op, 2, trans)) < 0 )
  78.             return code;
  79.         code = gs_translate(igs, trans[0], trans[1]);
  80.     }
  81.     else                /* matrix operand */
  82.     {    gs_matrix *pmat = (gs_matrix *)op->value.refs;
  83.         if ( (code = num_params(op - 1, 2, trans)) < 0 )
  84.             return code;
  85.         code = gs_make_translation(trans[0], trans[1], pmat);
  86.         op[-2] = *op;
  87.     }
  88.     if ( code >= 0 ) pop(2);
  89.     return code;
  90. }
  91.  
  92. /* <sx> <sy> scale - */
  93. /* <sx> <sy> <matrix> scale <matrix> */
  94. int
  95. zscale(register os_ptr op)
  96. {    float scale[2];
  97.     int code = write_matrix(op);
  98.     if ( code < 0 )            /* no matrix operand */
  99.     {    if ( (code = num_params(op, 2, scale)) < 0 )
  100.             return code;
  101.         code = gs_scale(igs, scale[0], scale[1]);
  102.     }
  103.     else                /* matrix operand */
  104.     {    gs_matrix *pmat = (gs_matrix *)op->value.refs;
  105.         if ( (code = num_params(op - 1, 2, scale)) < 0 )
  106.             return code;
  107.         code = gs_make_scaling(scale[0], scale[1], pmat);
  108.         op[-2] = *op;
  109.     }
  110.     if ( code >= 0 ) pop(2);
  111.     return code;
  112. }
  113.  
  114. /* <angle> rotate - */
  115. /* <angle> <matrix> rotate <matrix> */
  116. int
  117. zrotate(register os_ptr op)
  118. {    int code = write_matrix(op);
  119.     float ang;
  120.     if ( code < 0 )            /* no matrix operand */
  121.        {    if ( (code = num_params(op, 1, &ang)) < 0 ) return code;
  122.         code = gs_rotate(igs, ang);
  123.        }
  124.     else                /* matrix operand */
  125.        {    gs_matrix *pmat = (gs_matrix *)op->value.refs;
  126.         if ( (code = num_params(op - 1, 1, &ang)) < 0 ) return code;
  127.         code = gs_make_rotation(ang, pmat);
  128.         op[-1] = *op;
  129.        }
  130.     if ( code >= 0 ) pop(1);
  131.     return code;
  132. }
  133.  
  134. /* <matrix> concat - */
  135. int
  136. zconcat(register os_ptr op)
  137. {    gs_matrix mat;
  138.     int code = read_matrix(op, &mat);
  139.     if ( code < 0 ) return code;
  140.     code = gs_concat(igs, &mat);
  141.     if ( code < 0 ) return code;
  142.     pop(1);
  143.     return 0;
  144. }
  145.  
  146. /* <matrix1> <matrix2> <matrix> concatmatrix <matrix> */
  147. int
  148. zconcatmatrix(register os_ptr op)
  149. {    gs_matrix m1, m2;
  150.     int code;
  151.     if (    (code = read_matrix(op - 2, &m1)) < 0 ||
  152.         (code = read_matrix(op - 1, &m2)) < 0 ||
  153.         (code = write_matrix(op)) < 0 ||
  154.         (code = gs_matrix_multiply(&m1, &m2, (gs_matrix *)(op->value.refs))) < 0
  155.        ) return code;
  156.     op[-2] = *op;
  157.     pop(2);
  158.     return code;
  159. }
  160.  
  161. /* <x> <y> transform <xt> <yt> */
  162. /* <x> <y> <matrix> transform <xt> <yt> */
  163. int
  164. ztransform(register os_ptr op)
  165. {    return common_transform(op, gs_transform, gs_point_transform);
  166. }
  167.  
  168. /* <dx> <dy> dtransform <dxt> <dyt> */
  169. /* <dx> <dy> <matrix> dtransform <dxt> <dyt> */
  170. int
  171. zdtransform(register os_ptr op)
  172. {    return common_transform(op, gs_dtransform, gs_distance_transform);
  173. }
  174.  
  175. /* <xt> <yt> itransform <x> <y> */
  176. /* <xt> <yt> <matrix> itransform <x> <y> */
  177. int
  178. zitransform(register os_ptr op)
  179. {    return common_transform(op, gs_itransform, gs_point_transform_inverse);
  180. }
  181.  
  182. /* <dxt> <dyt> idtransform <dx> <dy> */
  183. /* <dxt> <dyt> <matrix> idtransform <dx> <dy> */
  184. int
  185. zidtransform(register os_ptr op)
  186. {    return common_transform(op, gs_idtransform, gs_distance_transform_inverse);
  187. }
  188.  
  189. /* Common logic for [i][d]transform */
  190. private int near
  191. common_transform(register os_ptr op,
  192.   int (*ptproc)(P4(gs_state *, floatp, floatp, gs_point *)),
  193.   int (*matproc)(P4(floatp, floatp, const gs_matrix *, gs_point *)))
  194. {    float opxy[2];
  195.     gs_point pt;
  196.     int code;
  197.     /* Optimize for the non-matrix case */
  198.     switch ( r_type(op) )
  199.        {
  200.     case t_real:
  201.         opxy[1] = op->value.realval;
  202.         break;
  203.     case t_integer:
  204.         opxy[1] = op->value.intval;
  205.         break;
  206.     case t_array:                /* might be a matrix */
  207.        {    gs_matrix mat;
  208.         gs_matrix *pmat = &mat;
  209.         if (    (code = read_matrix(op, pmat)) < 0 ||
  210.             (code = num_params(op - 1, 2, opxy)) < 0 ||
  211.             (code = (*matproc)(opxy[0], opxy[1], pmat, &pt)) < 0
  212.            ) return code;
  213.         op--;
  214.         pop(1);
  215.         goto out;
  216.        }
  217.     default:
  218.         return_error(e_typecheck);
  219.        }
  220.     switch ( r_type(op - 1) )
  221.        {
  222.     case t_real:
  223.         opxy[0] = (op - 1)->value.realval;
  224.         break;
  225.     case t_integer:
  226.         opxy[0] = (op - 1)->value.intval;
  227.         break;
  228.     default:
  229.         return_error(e_typecheck);
  230.        }
  231.     if ( (code = (*ptproc)(igs, opxy[0], opxy[1], &pt)) < 0 )
  232.         return code;
  233. out:    make_real(op - 1, pt.x);
  234.     make_real(op, pt.y);
  235.     return 0;
  236. }
  237.  
  238. /* <matrix> <inv_matrix> invertmatrix <inv_matrix> */
  239. int
  240. zinvertmatrix(register os_ptr op)
  241. {    gs_matrix m;
  242.     int code;
  243.     if (    (code = read_matrix(op - 1, &m)) < 0 ||
  244.         (code = write_matrix(op)) < 0 ||
  245.         (code = gs_matrix_invert(&m, (gs_matrix *)op->value.refs)) < 0
  246.        ) return code;
  247.     op[-1] = *op;
  248.     pop(1);
  249.     return code;
  250. }
  251.  
  252. /* ------ Initialization procedure ------ */
  253.  
  254. op_def zmatrix_op_defs[] = {
  255.     {"1concat", zconcat},
  256.     {"2dtransform", zdtransform},
  257.     {"3concatmatrix", zconcatmatrix},
  258.     {"1currentmatrix", zcurrentmatrix},
  259.     {"2idtransform", zidtransform},
  260.     {"2invertmatrix", zinvertmatrix},
  261.     {"2itransform", zitransform},
  262.     {"1rotate", zrotate},
  263.     {"2scale", zscale},
  264.     {"1setmatrix", zsetmatrix},
  265.     {"2transform", ztransform},
  266.     {"2translate", ztranslate},
  267.     op_def_end(init_identity_matrix)
  268. };
  269.